<?php
class searchController extends Zend_Controller_Action {
	public function init() {
		$ajaxContext = $this->_helper->getHelper('AjaxContext');
		$ajaxContext->addActionContext('searched','json');
		$ajaxContext->initContext();
	
	}
	public function indexAction() {
		$categories = Core_Business_Category::getAll();
		$listcate = array();
		foreach ($categories as $category) {
			$listcate[$category->Id] = $category->Name;
		}
		$this->view->categories = $listcate;
	}
	
	public function searchAction() {
	
	}
	
	public function searchedAction() {
		if($this->getRequest()->isGet()) {
			$searchkey = trim($this->getRequest()->getParam('key'));
			$searchkey = str_replace('*','%',$searchkey);
			$date = trim($this->getRequest()->getParam('date'));
			$query = array();
			if(isset($searchkey))
				$query["name like ?"] = $searchkey;
			
			if(isset($date))
				$query["created >= ?"] = $date;
				
			if($this->_helper->Acl->getUser()->getRole()==Core_Business_Rule::Customer)
				$query["CreatedBy = ?"]=$this->_helper->Acl->getUser()->Id;

			$result = Core_Business_Training::getTraining($query);
			$collection = array();
			foreach ($result as $item) {
				$collection[]= array('name'=>$item->Name);
			}
			$this->view->output = Zend_Json::encode($collection);
		}
	}
}